package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.List;

import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.ContactServiceActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.pay.Order;
import cn.edu.zjicm.wordsnet_d.pay.Product;
import cn.edu.zjicm.wordsnet_d.util.DateTimeUtils;
import cn.edu.zjicm.wordsnet_d.util.Utils;
import android.content.Context;
import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ConsumeRecordAdapter extends BaseAdapter {

	private List<Order> ordersList;
	private Context mContext;

	public ConsumeRecordAdapter(List<Order> ordersList, Context context) {
		this.ordersList = ordersList;
		this.mContext = context;
	}

	@Override
	public int getCount() {
		return ordersList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyHolder holder = null;
		
		if(convertView==null){
			holder = new MyHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.view_record_item, parent, false);
			holder.layout = (RelativeLayout) convertView.findViewById(R.id.record_item_layout);
			holder.divider = (TextView) convertView.findViewById(R.id.record_item_divider);
			holder.recordDate = (TextView) convertView.findViewById(R.id.record_date);
			holder.recordBean = (TextView) convertView.findViewById(R.id.record_bean);
			holder.recordContent = (TextView) convertView.findViewById(R.id.record_content);
			holder.recordState = (TextView) convertView.findViewById(R.id.record_state);
			holder.recordTypeImg = (ImageView) convertView.findViewById(R.id.record_type_img);
			convertView.setTag(holder);
		}else{
			holder = (MyHolder) convertView.getTag();
		}
		
		if (ordersList.size() == 1) {
			Utils.setListItemSingle(holder.layout, holder.divider);
		} else {
			if (position == 0) {
				Utils.setListItemTop(holder.layout);
			} else if (position == ordersList.size() - 1) {
				Utils.setListItemBottom(holder.layout, holder.divider);
			}
		}

		Order order = ordersList.get(position);
		Product product = WordFactory.getInstance(mContext).getProduct(order.getProductId());
		if (product.getProductType() == 0) {
			holder.recordContent.setText("充值 " + product.getPrice() + "元");
			holder.recordTypeImg.setImageResource(R.drawable.icon_alipay);
			holder.recordBean.setText("+" + product.getProductName());
		} else {
			holder.recordContent.setText("购买了" + WordFactory.getInstance(mContext).getBookName(AppPreference.Book.BOOK_ID[product.getId()]));
			holder.recordTypeImg.setImageResource(R.drawable.icon_book);
			holder.recordBean.setText("-" + product.getPrice() + "知米豆");
		}

		holder.recordDate.setText(DateTimeUtils.getTimeStr(order.getGmtModified()) + "");

		String stateText;
		// 处理中
		if (order.getTradeStatus() == 0) {
			stateText = "处理中...";
			holder.recordState.setText(stateText);
			holder.recordState.setTextColor(mContext.getResources().getColor(R.color.record_failed));
		} else if (order.getTradeStatus() == 2) { // 失败
			stateText = "充值失败，请联系客服";
			SpannableStringBuilder builder = new SpannableStringBuilder(stateText);
			builder.setSpan(new MyClickableSpan(), 5, stateText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			holder.recordState.setText(builder);
			holder.recordState.setTextColor(mContext.getResources().getColor(R.color.record_failed));
			holder.recordState.setFocusable(true);
			holder.recordState.setMovementMethod(LinkMovementMethod.getInstance());
		}

		return convertView;
	}

	class MyHolder{
		RelativeLayout layout;
		TextView divider,recordDate,recordBean,recordContent,recordState;
		ImageView recordTypeImg;
	}
	
	class MyClickableSpan extends ClickableSpan {

		@Override
		public void onClick(View widget) {
			Intent in = new Intent(mContext, ContactServiceActivity.class);
			mContext.startActivity(in);
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			ds.setColor(mContext.getResources().getColor(R.color.tab_indicator_text_bg_down));
			ds.setUnderlineText(true);
		}
	}

}
